<?php
/**
 * 生产环境数据库连接测试脚本
 * 用于测试远程数据库连接
 */

// 设置环境变量为生产环境
putenv('APP_ENV=production');

// 引入自动加载文件
require_once 'vendor/autoload.php';

echo "========================================\n";
echo "生产环境数据库连接测试\n";
echo "========================================\n";

try {
    // 引入数据库配置
    $config = require 'config/database.php';
    
    $dbConfig = $config['connections']['mysql'];
    
    echo "数据库配置信息:\n";
    echo "- 主机: {$dbConfig['hostname']}\n";
    echo "- 数据库: {$dbConfig['database']}\n";
    echo "- 用户名: {$dbConfig['username']}\n";
    echo "- 端口: {$dbConfig['hostport']}\n";
    echo "- 表前缀: {$dbConfig['prefix']}\n\n";
    
    // 创建数据库连接
    $dsn = "mysql:host={$dbConfig['hostname']};port={$dbConfig['hostport']};dbname={$dbConfig['database']};charset={$dbConfig['charset']}";
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);
    
    echo "✓ 数据库连接成功!\n\n";
    
    // 测试查询一些数据
    echo "测试查询数据...\n";
    
    // 查询用户表
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM oa_admin");
    $result = $stmt->fetch();
    echo "- 用户表记录数: {$result['count']}\n";
    
    // 查询权限规则表
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM oa_admin_rule");
    $result = $stmt->fetch();
    echo "- 权限规则记录数: {$result['count']}\n";
    
    // 查询客户表
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM oa_customer");
    $result = $stmt->fetch();
    echo "- 客户表记录数: {$result['count']}\n";
    
    // 查询前几个用户
    $stmt = $pdo->query("SELECT id, name, username FROM oa_admin LIMIT 3");
    $users = $stmt->fetchAll();
    echo "\n前3个用户:\n";
    foreach ($users as $user) {
        echo "- ID: {$user['id']}, 姓名: {$user['name']}, 用户名: {$user['username']}\n";
    }
    
    echo "\n✓ 生产环境数据库测试完成!\n";
    
} catch (PDOException $e) {
    echo "✗ 数据库连接失败: " . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "✗ 测试过程中发生错误: " . $e->getMessage() . "\n";
}